var modleTree,nodeTree;
$(function () {
	initNodeTree();
});

function initNodeTree(){
	var processDefinitionId=T.p("processDefinitionId");
	$.get("../bpm/flow/getProcessDefinitionNodeTree/"+processDefinitionId, function(r){
	var treeData = r.treeData;
	var setting = {
		    data: {
		        simpleData: {
		            enable: true,
		            idKey: "treeId",
		            pIdKey: "treePid",
		            rootPId: -1
		        },
		        key: {
		            url:"nourl",
		            name:"treeName"
		        }
		    },
		    callback:{
		    	onClick:function(event, treeId, treeNode) {
		    		initNodeRuleTree(treeNode.treeId);
		    		vm.nodeId=treeNode.treeId;
		    		vm.ruleId='0';
		    	},
		    }
		};
	modleTree = $.fn.zTree.init($("#nodeTree"), setting, treeData);
	modleTree.expandAll(true);
 });
}
function initNodeRuleTree(nodeId){
	var processDefinitionId=T.p("processDefinitionId");
	$.get("../bpm/flow/getProcessDefinitionNodeRuleTree/"+processDefinitionId+"/"+nodeId, function(r){
	var treeData= r.treeData;
	treeData.push({id:'0'+processDefinitionId,ruleName:'规则设置',pid:""});//添加一个根
	var setting = {
		    data: {
		        simpleData: {
		            enable: true,
		            idKey: "id",
		            pIdKey: "pid",
		            rootPId: -1
		        },
		        key: {
		            url:"nourl",
		            name:"ruleName"
		        }
		    },
		    callback:{
		    	onClick:function(event, treeId, treeNode) {
		    		if (treeNode.id!='0') {
		    			vm.update(treeNode.id);
		    			vm.ruleId=treeNode.id;
					}else{
						vm.reload();
					}
		    	},
		    }
		};
	nodeTree = $.fn.zTree.init($("#ruleTree"), setting, treeData);
	nodeTree.expandAll(true);
 });
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		nodeId:'',
		ruleId:'',
		bpmModel: {processDefinitionId:T.p("processDefinitionId")},
		bpmNodeRule:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		getInfo: function(id){
			$.get("../bpmnoderule/info/"+id, function(r){
                vm.bpmNodeRule = r.bpmNodeRule;
            });
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			var pid=this.ruleId;
			var bpmModleNodeId=this.nodeId;
			vm.bpmNodeRule = {bpmProcessDefinitionId:T.p("processDefinitionId"),pid:pid,ruleType:'b',bpmModleNodeId:bpmModleNodeId};
		},
		update: function (id) {
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.bpmNodeRule.id == null ? "../bpmnoderule/save" : "../bpmnoderule/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.bpmNodeRule),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			initNodeRuleTree(this.nodeId);
		}
	}
});